<template>
  <div class="home">
<el-container class="home-container">
    <!-- 头部区域  -->
     <el-header>
          <div class="header-name">
            <img src="../assets/logo.png">
            <span>后台管理系统</span>
          </div>
           <el-button type="info" @click="logout">退出</el-button>
     </el-header>
 <el-container>
           
                 <!-- 左边导航栏  -->
                  <el-aside :width="isCossaple?'64px':'300px'">
            <div class="toggle-button" @click="togetcollapse">|||</div>
            
            <!-- unique-opened 一级菜单只能开启一个  collapse 折叠    router 开启路由模式   default-active 路由高亮-->
           <el-menu unique-opened :collapse=isCossaple :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <!-- :index="item.id+' '" 只展开一个菜单 :index只接受字符串 -->
          <el-submenu :index="item.id+' '" v-for="item in menulis" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class='iconsObj[item.id]'></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <!-- @click="staticPath('/'+subItem.path) 点击二级菜单把值保存到 sessionStorage 中 下次刷新也会打开 -->
            <el-menu-item :index="'/'+subItem.path" :key="index" v-for="(subItem, index) in item.children"
             @click="staticPath('/'+subItem.path)">
              <template>
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>

          </el-submenu>
           </el-menu>
           </el-aside >
                <!-- 左侧导航结束   -->

                    <!-- 右边边主要内容 -->
                  <el-main>
                     <!-- 路由占位符 -->
                        <router-view></router-view>
                  </el-main>
                  
               </el-container>
  </el-container>
  </div>
</template>

<script>
// @ is an alias to /src
// import login from '@/components/login.vue'

export default {
  data() {
    return {
      //  左侧菜单数组
       menulis:[],
       iconsObj:{
         '125':'el-icon-s-custom',
         '103':'el-icon-wallet',
         '101':'el-icon-present',
         '102':'el-icon-tickets',
         '145':'el-icon-edit-outline',
       },
       isCossaple:false, // 是否折叠
       activePath:''  // 链接激活
    }
  },
created(){
 this.getMenuList()
  //  链接激活
    this.activePath=window.sessionStorage.getItem('vall')
},
  methods:{
    // 退出登录只需要销毁本地的 token 即可。这样，后续的请求就不会携带
    // token, 必须重新登录生成一个新的 token 之后才可以访问页面
    logout(){
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //获取所有的菜单
     async getMenuList(){
      const {data:res} = await this.$http.get('menus')
      if(res.meta.status!==200) return $message.error(res.meat.msg);
      // console.log(res.data);
      this.menulis=res.data
    },
    // 折叠菜单
    togetcollapse(){
      this.isCossaple= !this.isCossaple;
    },
    // 保存链接的激活状态
    staticPath(val){
         window.sessionStorage.setItem('vall', val)  // 存入的值 val
        //  当点击其他的二级菜单重新赋值
         this.activePath=val
    }
  }

}
</script>

<style  scoped>
html,
body,
#app{
    height: 100%;
    margin: 0;
    padding: 0;
}
.home{
  height: 100%;
}
  .home-container{
  width:100%;
  height: 100%;
}
.el-header{
background-color: #3a5a6e;
display: flex;
justify-content: space-between;
align-items: center;
}
.header-name{
display: flex;
justify-content: center;
align-items: center;
}
.toggle-button {
display: flex;
justify-content: center;
align-items:center;
}

span{
  
  margin-left: 15px;
  color:rgb(101, 128, 170);

}
.el-header img{
  width: 40px;
  height: 40px;
}
.el-aside{
  background-color: #85a2b5;
}  
  .el-menu{
    border-right:none;
    /* height: 300px; */
  }

.el-main{
  background-color: #85a2b5;
  height: 100;
}
.toggle-button{
  background-color: #85a2b5;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2rem;
  cursor: pointer;
}
</style>
